import * as React from 'react';
import { Props, State } from './type';
import './index.less';
import { CurrencyArray } from '../CommonProps';
import classnames from 'classnames';

export class FormItem extends React.Component<Props, State> {
  public static defaultProps = new Props();
  public state = new State();
  public currentUnit?: string = ''; // 当前数值单位

  getCurrentUnit = (unit: string) => {
    if (!unit) return;
    const currency = CurrencyArray.find((ele) => ele.value === unit)
      ?.currencyType;
    this.currentUnit = currency ? currency : unit;
  };

  public render() {
    const prefix = 'customFormItem';
    const {
      code,
      form,
      unit,
      explainText,
      warningText
    } = this.props;
    // bpmnPermission
    const error = form?.getFieldError(code);
    this.getCurrentUnit(unit);
    let borderCls = 'ignore-ratio-border-bottom-left-15';
    if (this.props.renderMode === 'detail') {
      // 详情模式不需要底部 border
      borderCls = '';
    }
    const cls = classnames(
      'customFormItem',
      borderCls,
      this.props.editSetting.key.split('-')[1],
      {
        customFormItemError: error && error.length
      }
    );

    return (
      <div className={cls}>
        {/* {name ? (
          <div
            title={!!this.currentUnit ? name + `(${this.currentUnit})` : name}
            className={`${prefix}Label ${prefix +
              (required ? 'Required' : 'NoRequired')}`}
          >
            {!!this.currentUnit ? name + `(${this.currentUnit})` : name}
          </div>
        ) : null} */}
        <div className={`${prefix}Right`}>
          <div className={`${prefix}Content`}>{this.props.children}</div>
          {explainText ? (
            <div className={`${prefix}ExplainText`}>{explainText}</div>
          ) : null}
          {warningText ? (
            <div className={`${prefix}warningText`}>{warningText}</div>
          ) : null}
          {/* {error && error.length ? (
            <div className={`${prefix}Error`}>{error[0]}</div>
          ) : null} */}
        </div>
      </div>
    );
  }
}
